package com.migration.clientws;

import com.ulatina.server.Column;
import com.ulatina.server.NewService;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.PhaseId;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

/**
 *
 * @author stevenziggiz
 */
@ManagedBean(name = "beanConexionDB")
@ApplicationScoped
public class ConexionDB {

    private String sourceDataBaseType;
    private String sourceIpServer;
    private int sourcePort;
    private String sourceServiceName;
    private String sourceDataBaseName;
    private String sourceUser;
    private String sourcePassword;
    private String destinyDataBaseType;
    private String destinyIpServer;
    private int destinyPort;
    private String destinyServiceName;
    private String destinyDataBaseName;
    private String destinyUser;
    private String destinyPassword;
    //tabla selection
    private List<String> sourceTablesList;
    private List<String> destinyTablesList;
    private String sourceTable;
    private String destinyTable;
    private List<Column> sourceColumnsList;
    private List<Column> destinyColumnsList;
    //pantalla 3 - Resultado
    private Boolean resultado;
    private String resultadoString;

    /**
     * Creates a new instance of ConexionDB
     */
    public ConexionDB() {
        sourceIpServer = "localhost";
        sourcePort = 3306;
        sourceServiceName = "";
        sourceDataBaseName = "agilefalcon";
        sourceUser = "root";
        sourcePassword = "Admin123";
        destinyIpServer = "localhost";
        destinyPort = 3306;
        destinyServiceName = "";
        destinyDataBaseName = "agilefalcon";
        destinyUser = "root";
        destinyPassword = "Admin123";
        resultadoString = "Fallida";
    }

    public String conectar() {
        try {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.getInInterceptors().add(new LoggingInInterceptor());
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setServiceClass(NewService.class);
            factory.setAddress("http://localhost:7001/WebService5/NewService");
            NewService migrationService = (NewService) factory.create();
            sourceTablesList = migrationService.getTablesList(sourceIpServer, sourcePort, sourceServiceName, sourceDataBaseName, sourceUser, sourcePassword, sourceDataBaseType);
            sourceColumnsList = migrationService.getTableColumnList(sourceIpServer, sourcePort, sourceServiceName, sourceDataBaseName, sourceUser, sourcePassword, sourceDataBaseType, sourceTablesList.get(0));
            destinyTablesList = migrationService.getTablesList(destinyIpServer, destinyPort, destinyServiceName, destinyDataBaseName, destinyUser, destinyPassword, destinyDataBaseType);
            destinyColumnsList = migrationService.getTableColumnList(destinyIpServer, destinyPort, destinyServiceName, destinyDataBaseName, destinyUser, destinyPassword, destinyDataBaseType, destinyTablesList.get(0));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "tableSelection";
    }

    public void cargarSourceColumns() {
        try {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.getInInterceptors().add(new LoggingInInterceptor());
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setServiceClass(NewService.class);
            factory.setAddress("http://localhost:7001/WebService5/NewService");
            NewService migrationService = (NewService) factory.create();
            sourceColumnsList = migrationService.getTableColumnList(sourceIpServer, sourcePort, sourceServiceName, sourceDataBaseName, sourceUser, sourcePassword, sourceDataBaseType, sourceTable);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void cargarDestinyColumns() {
        try {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.getInInterceptors().add(new LoggingInInterceptor());
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setServiceClass(NewService.class);
            factory.setAddress("http://localhost:7001/WebService5/NewService");
            NewService migrationService = (NewService) factory.create();
            destinyColumnsList = migrationService.getTableColumnList(destinyIpServer, destinyPort, destinyServiceName, destinyDataBaseName, destinyUser, destinyPassword, destinyDataBaseType, destinyTable);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void seleccionarUsuario(AjaxBehaviorEvent evento) {
        if (evento.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
            evento.setPhaseId(PhaseId.INVOKE_APPLICATION);
            evento.queue();
        } else {
            if (sourceTable != null) {
                System.out.println("Si");
            }
        }
    }

    public String migrar() {
        try {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.getInInterceptors().add(new LoggingInInterceptor());
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setServiceClass(NewService.class);
            factory.setAddress("http://localhost:7001/WebService5/NewService");
            NewService migrationService = (NewService) factory.create();
            resultado = migrationService.migrarDatos(sourceColumnsList, destinyColumnsList, sourceTable, destinyTable, sourceIpServer, sourcePort, sourceServiceName, sourceDataBaseName, sourceUser, sourcePassword, sourceDataBaseType, destinyIpServer, destinyPort, destinyServiceName, destinyDataBaseName, destinyUser, destinyPassword, destinyDataBaseType);
            resultadoString = (resultado) ? "Exitosa" : "Fallida";
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "resultado";
    }

    public String home() {
        return "home";
    }

    /**
     * @return the sourceIpServer
     */
    public String getSourceIpServer() {
        return sourceIpServer;
    }

    /**
     * @param sourceIpServer the sourceIpServer to set
     */
    public void setSourceIpServer(String sourceIpServer) {
        this.sourceIpServer = sourceIpServer;
    }

    /**
     * @return the sourcePort
     */
    public int getSourcePort() {
        return sourcePort;
    }

    /**
     * @param sourcePort the sourcePort to set
     */
    public void setSourcePort(int sourcePort) {
        this.sourcePort = sourcePort;
    }

    /**
     * @return the sourceServiceName
     */
    public String getSourceServiceName() {
        return sourceServiceName;
    }

    /**
     * @param sourceServiceName the sourceServiceName to set
     */
    public void setSourceServiceName(String sourceServiceName) {
        this.sourceServiceName = sourceServiceName;
    }

    /**
     * @return the sourceDataBaseName
     */
    public String getSourceDataBaseName() {
        return sourceDataBaseName;
    }

    /**
     * @param sourceDataBaseName the sourceDataBaseName to set
     */
    public void setSourceDataBaseName(String sourceDataBaseName) {
        this.sourceDataBaseName = sourceDataBaseName;
    }

    /**
     * @return the sourceUser
     */
    public String getSourceUser() {
        return sourceUser;
    }

    /**
     * @param sourceUser the sourceUser to set
     */
    public void setSourceUser(String sourceUser) {
        this.sourceUser = sourceUser;
    }

    /**
     * @return the sourcePassword
     */
    public String getSourcePassword() {
        return sourcePassword;
    }

    /**
     * @param sourcePassword the sourcePassword to set
     */
    public void setSourcePassword(String sourcePassword) {
        this.sourcePassword = sourcePassword;
    }

    /**
     * @return the destinyIpServer
     */
    public String getDestinyIpServer() {
        return destinyIpServer;
    }

    /**
     * @param destinyIpServer the destinyIpServer to set
     */
    public void setDestinyIpServer(String destinyIpServer) {
        this.destinyIpServer = destinyIpServer;
    }

    /**
     * @return the destinyPort
     */
    public int getDestinyPort() {
        return destinyPort;
    }

    /**
     * @param destinyPort the destinyPort to set
     */
    public void setDestinyPort(int destinyPort) {
        this.destinyPort = destinyPort;
    }

    /**
     * @return the destinyServiceName
     */
    public String getDestinyServiceName() {
        return destinyServiceName;
    }

    /**
     * @param destinyServiceName the destinyServiceName to set
     */
    public void setDestinyServiceName(String destinyServiceName) {
        this.destinyServiceName = destinyServiceName;
    }

    /**
     * @return the destinyDataBaseName
     */
    public String getDestinyDataBaseName() {
        return destinyDataBaseName;
    }

    /**
     * @param destinyDataBaseName the destinyDataBaseName to set
     */
    public void setDestinyDataBaseName(String destinyDataBaseName) {
        this.destinyDataBaseName = destinyDataBaseName;
    }

    /**
     * @return the destinyUser
     */
    public String getDestinyUser() {
        return destinyUser;
    }

    /**
     * @param destinyUser the destinyUser to set
     */
    public void setDestinyUser(String destinyUser) {
        this.destinyUser = destinyUser;
    }

    /**
     * @return the destinyPassword
     */
    public String getDestinyPassword() {
        return destinyPassword;
    }

    /**
     * @param destinyPassword the destinyPassword to set
     */
    public void setDestinyPassword(String destinyPassword) {
        this.destinyPassword = destinyPassword;
    }

    /**
     * @return the sourceDataBaseType
     */
    public String getSourceDataBaseType() {
        return sourceDataBaseType;
    }

    /**
     * @param sourceDataBaseType the sourceDataBaseType to set
     */
    public void setSourceDataBaseType(String sourceDataBaseType) {
        this.sourceDataBaseType = sourceDataBaseType;
    }

    /**
     * @return the destinyDataBaseType
     */
    public String getDestinyDataBaseType() {
        return destinyDataBaseType;
    }

    /**
     * @param destinyDataBaseType the destinyDataBaseType to set
     */
    public void setDestinyDataBaseType(String destinyDataBaseType) {
        this.destinyDataBaseType = destinyDataBaseType;
    }

    /**
     * @return the sourceTablesList
     */
    public List<String> getSourceTablesList() {
        return sourceTablesList;
    }

    /**
     * @param sourceTablesList the sourceTablesList to set
     */
    public void setSourceTablesList(List<String> sourceTablesList) {
        this.sourceTablesList = sourceTablesList;
    }

    /**
     * @return the destinyTablesList
     */
    public List<String> getDestinyTablesList() {
        return destinyTablesList;
    }

    /**
     * @param destinyTablesList the destinyTablesList to set
     */
    public void setDestinyTablesList(List<String> destinyTablesList) {
        this.destinyTablesList = destinyTablesList;
    }

    /**
     * @return the sourceTable
     */
    public String getSourceTable() {
        return sourceTable;
    }

    /**
     * @param sourceTable the sourceTable to set
     */
    public void setSourceTable(String sourceTable) {
        this.sourceTable = sourceTable;
    }

    /**
     * @return the destinyTable
     */
    public String getDestinyTable() {
        return destinyTable;
    }

    /**
     * @param destinyTable the destinyTable to set
     */
    public void setDestinyTable(String destinyTable) {
        this.destinyTable = destinyTable;
    }

    /**
     * @return the sourceColumnsList
     */
    public List<Column> getSourceColumnsList() {
        return sourceColumnsList;
    }

    /**
     * @param sourceColumnsList the sourceColumnsList to set
     */
    public void setSourceColumnsList(List<Column> sourceColumnsList) {
        this.sourceColumnsList = sourceColumnsList;
    }

    /**
     * @return the destinyColumnsList
     */
    public List<Column> getDestinyColumnsList() {
        return destinyColumnsList;
    }

    /**
     * @param destinyColumnsList the destinyColumnsList to set
     */
    public void setDestinyColumnsList(List<Column> destinyColumnsList) {
        this.destinyColumnsList = destinyColumnsList;
    }

    /**
     * @return the resultadoString
     */
    public String getResultadoString() {
        return resultadoString;
    }

    /**
     * @param resultadoString the resultadoString to set
     */
    public void setResultadoString(String resultadoString) {
        this.resultadoString = resultadoString;
    }
}
